//
//  StatusDetailTopToolBar.m
//  Weibo
//
//  Created by Anasue on 10/3/15.
//  Copyright © 2015 vvusu. All rights reserved.
//

#import "StatusDetailTopToolBar.h"
#import "Status.h"

@interface StatusDetailTopToolBar ()<StatusDetailTopToolBarDelegate>
@property (weak, nonatomic) IBOutlet UIButton *retweetedBtn;
@property (weak, nonatomic) IBOutlet UIButton *commentBtn;
@property (weak, nonatomic) IBOutlet UIButton *attitudeBtn;
@property (weak, nonatomic) IBOutlet UIImageView *littleArrow;
@property (nonatomic, weak) UIButton *selectedBtn;
- (IBAction)btnClick:(UIButton *)btn;

@end

@implementation StatusDetailTopToolBar

+ (instancetype)toolBar
{
    return [[[NSBundle mainBundle]loadNibNamed:@"StatusDetailTopToolBar" owner:nil options:nil]lastObject];
}


- (void)drawRect:(CGRect)rect {
    [[UIImage resizeImage:@"statusdetail_comment_top_background"]drawInRect:rect];
}

- (void)awakeFromNib
{
    self.retweetedBtn.tag = StatusDetailTopToolBarRetweetedBtn;
    self.commentBtn.tag = StatusDetailTopToolBarCommentBtn;
    self.backgroundColor = Color(211, 211, 211);
}
- (IBAction)btnClick:(UIButton *)btn
{
    self.selectedBtn.selected = NO;
    btn.selected = YES;
    self.selectedBtn = btn;
    [UIView animateWithDuration:0.2 animations:^{
        self.littleArrow.centerX = self.selectedBtn.centerX;
    }];
    if ([self.delegate respondsToSelector:@selector(statusDetailTopToolBar:didSelectedBtn:)]) {
        [self.delegate statusDetailTopToolBar:self didSelectedBtn:btn.tag];
    }
}

- (void)setDelegate:(id<StatusDetailTopToolBarDelegate>)delegate
{
    _delegate = delegate;
    UIButton *btn = (UIButton *)[self viewWithTag:StatusDetailTopToolBarCommentBtn];
    [self btnClick:btn];
}

- (void)setStatus:(Status *)status
{
    _status = status;
    [self setupBtnTitle:self.retweetedBtn count:status.reposts_count defaultTitle:@"转发"];
    [self setupBtnTitle:self.commentBtn count:status.comments_count defaultTitle:@"评论"];
    [self setupBtnTitle:self.attitudeBtn count:status.attitudes_count defaultTitle:@"赞"];
}

- (void)setupBtnTitle:(UIButton *)button count:(int)count defaultTitle:(NSString *)defaultTitle
{
    if (count >=10000) {
        defaultTitle = [NSString stringWithFormat:@"%@ %d.1f万",defaultTitle,count / 10000];
        defaultTitle = [defaultTitle stringByReplacingOccurrencesOfString:@".0" withString:@""];
    }else if (count > 0){
        defaultTitle = [NSString stringWithFormat:@"%@ %d",defaultTitle,count];
    }else{
        defaultTitle = [NSString stringWithFormat:@"%@ 0",defaultTitle];
    }
    [button setTitle:defaultTitle forState:UIControlStateNormal];
}

@end
